//
//  YDStockMarketDiskDataStateView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/1/19.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDStockMarketDiskDataStateView.h"


@interface YDStockMarketDiskDataStateView()

//交易状态
@property (nonatomic , weak) UILabel *stateLabel;
//时间
@property (nonatomic , weak) UILabel *timeLabel;
//大盘指数
@property (nonatomic , weak) UILabel *marketLabel;
//涨跌标志
@property (nonatomic , weak) UIImageView *marketImage;
//涨跌幅度
@property (nonatomic , weak) UILabel *upSpeed;
//涨跌百分比
@property (nonatomic , weak) UILabel *upSpeedRate;

@end

@implementation YDStockMarketDiskDataStateView

-(void)setStockDict:(NSDictionary *)stockDict
{
    _stockDict = stockDict;
    
    DDLog(@"stockDict = %@" , stockDict)
    /*
     askPrice2 : 0,
     upTime : 1516604463000,
     preClsPrice : 3487.86,
     bidPrice4 : 0,
     askPrice3 : 0,
     bidPrice5 : 0,
     highestPrice : 3503.39,
     instrumentId : 1A0001,
     upLimitPrice : 0,
     openPrice : 3476.99,
     tradeDay : 2018-01-22,
     upDropPrice : 13.5,
     volume : 217487534,
     upTimeFormat : 2018-01-22 15:01:03,
     lastPrice : 3501.36,
     askPrice4 : 0,
     turnover : 261295714088,
     name : 上证指数,
     bidPrice : 0,
     bidPrice2 : 0,
     favorite : 0,
     status : 1,
     isTradeTime : 0,
     lowestPrice : 3475.67,
     askPrice5 : 0,
     bidPrice3 : 0,
     upDropSpeed : 0.0039,
     askPrice : 0,
     downLimitPrice : 0,
     exchangeId : 4353
     */
    
    //是否交易时间段
    if ([stockDict[@"isTradeTime"] boolValue]) {
        self.stateLabel.text = @"交易中";
    }else{
        self.stateLabel.text = @"休市中";
    }
    
    //时间
    //upTimeFormat : 2018-01-22 15:01:03,
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    [formate1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDateFormatter *formate2 = [[NSDateFormatter alloc] init];
    [formate2 setDateFormat:@"MM/dd"];
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@" , [formate2 stringFromDate:[formate1 dateFromString:[NSString stringWithFormat:@"%@" , stockDict[@"upTimeFormat"]]]]];
    
    
    
    //当前价 , 变化值 变化幅度
    NSString *lastPriceString;
    if (stockDict[@"lastPrice"]) {
        NSString *lastPrice = [[NSString stringWithFormat:@"%@" , stockDict[@"lastPrice"]] pz_stringNumberByMultiplyingBy:@"1"];
        lastPriceString = [NSString stringWithFormat:@"%@" , lastPrice];
    }else{
        lastPriceString = [NSString stringWithFormat:@"---"];
    }
    self.marketLabel.text = lastPriceString;
    
    NSString *upDropPriceString;
    if (stockDict[@"upDropPrice"]) {
        NSString *upDropPrice = [[NSString stringWithFormat:@"%@" , stockDict[@"upDropPrice"]] pz_stringNumberByMultiplyingBy:@"1"];
        upDropPriceString = [NSString stringWithFormat:@"%@" , upDropPrice];
    }else{
        upDropPriceString = [NSString stringWithFormat:@"---"];
    }
    self.upSpeed.text = upDropPriceString;
    
    
    NSString *upDropSpeedString;
    if (stockDict[@"upDropSpeed"]) {
        NSString *upDropSpeed = [[NSString stringWithFormat:@"%@" , stockDict[@"upDropSpeed"]] pz_stringNumberByMultiplyingBy:@"100"];
        upDropSpeedString = [NSString stringWithFormat:@"%@%@" , upDropSpeed , @"%"];
    }else{
        upDropSpeedString = [NSString stringWithFormat:@"---"];
    }
    self.upSpeedRate.text = upDropSpeedString;
    
    //颜色设置
    if (stockDict[@"upDropSpeed"] && [stockDict[@"upDropSpeed"] floatValue] >0) {
        self.marketLabel.textColor = [UIColor colorWithHexString:stockRed];
        self.upSpeed.textColor = [UIColor colorWithHexString:stockRed];
        self.upSpeedRate.textColor = [UIColor colorWithHexString:stockRed];
        self.marketImage.image = [UIImage imageNamed:@"marketDetailup"];
    }else{
        self.marketLabel.textColor = [UIColor colorWithHexString:stockGreen];
        self.upSpeed.textColor = [UIColor colorWithHexString:stockGreen];
        self.upSpeedRate.textColor = [UIColor colorWithHexString:stockGreen];
        self.marketImage.image = [UIImage imageNamed:@"marketDetaildown"];
    }
    
    
    
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self stateLabel];
        [self timeLabel];
        [self marketLabel];
        [self marketImage];
        [self upSpeedRate];
        [self upSpeed];
    }
    return self;
}
+(instancetype)stockMarketDiskDataStateViewWithStockDict:(NSDictionary *)stockDict
{
    YDStockMarketDiskDataStateView *detailView = [[YDStockMarketDiskDataStateView alloc] init];
    detailView.stockDict = stockDict;
    return detailView;
}

-(UILabel *)stateLabel
{
    if (_stateLabel == nil) {
        UILabel *stateLabel = [[UILabel alloc] init];
        [self addSubview:stateLabel];
        stateLabel.textColor = [UIColor colorWithHexString:dark];
        stateLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        stateLabel.textAlignment = NSTextAlignmentLeft;
        stateLabel.text = @"---";
        [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(rate375(20));
            make.bottom.equalTo(self.mas_centerY);
        }];
        _stateLabel = stateLabel;
    }
    return _stateLabel;
}
-(UILabel *)timeLabel
{
    if (_timeLabel == nil) {
        UILabel *timeLabel = [[UILabel alloc] init];
        [self addSubview:timeLabel];
        timeLabel.textColor = [UIColor colorWithHexString:@"687284"];
        timeLabel.font = [UIFont systemFontOfSize:font10ScreenWRate * LKScreenW];
        timeLabel.textAlignment = NSTextAlignmentLeft;
        timeLabel.text = @"---";
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stateLabel);
            make.top.equalTo(self.mas_centerY);
        }];
        _timeLabel = timeLabel;
    }
    return _timeLabel;
}

-(UILabel *)marketLabel
{
    if (_marketLabel == nil) {
        UILabel *marketLabel = [[UILabel alloc] init];
        [self addSubview:marketLabel];
        marketLabel.textColor = NumberColor(222, 95, 65);
        marketLabel.font = [UIFont boldSystemFontOfSize:font24ScreenWRate * LKScreenW];
        marketLabel.textAlignment = NSTextAlignmentCenter;
        marketLabel.text = @"---";
        [marketLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.centerX.equalTo(self.mas_centerX).offset(-rate375(20));
        }];
        _marketLabel = marketLabel;
    }
    return _marketLabel;
}

-(UIImageView *)marketImage
{
    if (_marketImage == nil) {
        UIImageView *marketImage = [[UIImageView alloc] init];
        [self addSubview:marketImage];
        marketImage.image = [UIImage imageNamed:@"marketDetailup"];
        [marketImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self.marketLabel.mas_right).offset(rate375(5));
        }];
        
        _marketImage = marketImage;
    }
    return _marketImage;
}


-(UILabel *)upSpeedRate
{
    if (_upSpeedRate == nil) {
        UILabel *upSpeedRate = [[UILabel alloc] init];
        [self addSubview:upSpeedRate];
        upSpeedRate.textColor = NumberColor(222, 95, 65);
        upSpeedRate.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        upSpeedRate.textAlignment = NSTextAlignmentRight;
        upSpeedRate.text = @"---";
        [upSpeedRate mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-rate375(20));
            make.centerY.equalTo(self);
            make.width.equalTo(@(rate375(50)));
        }];
        upSpeedRate.adjustsFontSizeToFitWidth = YES;
        _upSpeedRate = upSpeedRate;
    }
    return _upSpeedRate;
}

-(UILabel *)upSpeed
{
    if (_upSpeed == nil) {
        UILabel *upSpeed = [[UILabel alloc] init];
        [self addSubview:upSpeed];
        upSpeed.textColor = NumberColor(222, 95, 65);
        upSpeed.font = [UIFont boldSystemFontOfSize:font12ScreenWRate * LKScreenW];
        upSpeed.textAlignment = NSTextAlignmentRight;
        upSpeed.text = @"---";
        [upSpeed mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.upSpeedRate.mas_left).offset(-rate375(10));
            make.centerY.equalTo(self);
            make.width.equalTo(@(rate375(60)));
        }];
        upSpeed.adjustsFontSizeToFitWidth = YES;
        _upSpeed = upSpeed;
    }
    return _upSpeed;
}


@end
